package com.blb.service.impl;

import com.blb.entity.IOrderLaser;
import com.blb.entity.OrderLaser;
import com.blb.entity.OrderLog;
import com.blb.entity.StayProduction;
import com.blb.mapper.OrderLaserMapper;
import com.blb.service.IOrderLaserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.service.IOrderLogService;
import com.blb.utils.TimeUtils;
import com.blb.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tb
 * @since 2020-05-25
 */
@Service
public class OrderLaserServiceImpl extends ServiceImpl<OrderLaserMapper, OrderLaser> implements IOrderLaserService {

    @Resource
    private OrderLaserMapper orderLaserMapper;

    @Autowired
    private IOrderLogService orderLogService;


    /**
     * 根据订单编号修改状态
     * @param orderNo
     * @return
     */
    @Override
    public boolean updateStatus(String orderNo) {
        return orderLaserMapper.updateStatus(orderNo);
    }

    /**
     * 根据订单号查询列表
     * @param orderNo
     * @return
     */
    @Override
    public List<IOrderLaser> getByOrderNo(String orderNo) {
        return orderLaserMapper.getByOrderNo(orderNo);
    }

    /**
     * 根据订单号查询总条数
     * @param orderNo
     * @return
     */
    @Override
    public int countByOrderNo(String orderNo) {
        return orderLaserMapper.countByOrderNo(orderNo);
    }

    /**
     * 手动分配打印机，根据id修改状态为已打印
     * @param id
     * @param printerId
     * @return
     */
    @Override
    public boolean updateStatusById(String id, String printerId) {
        return orderLaserMapper.updateStatusById(id,printerId);
    }

    /**
     * 待生产订单列表
     * @return
     */
    @Override
    public List<StayProduction> Ilist(Integer start, Integer length,String companyId) {
        return orderLaserMapper.Ilist(start, length,companyId);
    }

    /**
     * 根据订单号查待生产订单
     * @return
     */
    @Override
    public List<StayProduction> queryByOrderNo(String orderNo,String companyId) {
        return orderLaserMapper.queryByOrderNo(orderNo,companyId);
    }

    /**
     * 根据订单编号修改待生产订单的生产状态
     * @param orderNo
     * @return
     */
    @Override
    public boolean updateStayByOrderIdToState(String orderNo) {
        return orderLaserMapper.updateStayByOrderIdToState(orderNo);
    }

    /**
     * 根据id修改订单激光码表的打印机和状态
     * @param id
     * @param lpId
     * @return
     */
    @Override
    public boolean updateByIdToLpIdStatus(String id, String lpId) {
        return orderLaserMapper.updateByIdToLpIdStatus(id,lpId);
    }

    /**
     * 根据订单号查询未打印个数
     * @param orderNo
     * @return
     */
    @Override
    public int productionIsEndByOrderNo(String orderNo) {
        return orderLaserMapper.productionIsEndByOrderNo(orderNo);
    }

    /**
     * 生成激光码
     * @param codeList
     * @return
     */
    @Override
    @Transactional
    public boolean generate(List<OrderLaser> codeList) {
        try {
            for(OrderLaser ol : codeList){
                ol.setId(UUIDUtils.getId());
                ol.setOlStatus("待打印");
                //修改激光码表的状态为已生成
                updateStatus(ol.getOrderNo());
                //向订单激光码表中添加数据
                save(ol);
            }
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderTime(TimeUtils.getNowTime());
            orderLog.setOrderId(codeList.get(0).getOrderNo());
            orderLog.setProduction("生产中");
            orderLogService.save(orderLog);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

        return true;
    }


    /**
     * 确定打印并添加日志
     * @param list
     * @return
     */
    @Override
    @Transactional
    public boolean confirmPrintingAndLog(List<IOrderLaser> list) {
        try {
            for(IOrderLaser iol : list){
                OrderLaser ol = new OrderLaser();
                ol.setId(iol.getId());
                ol.setOlStatus("已打印");
                ol.setOlCode(iol.getOlCode());
                ol.setOrderNo(iol.getOrderNo());
                ol.setPrinterId(iol.getPrinterId());
                updateById(ol);
            }

            IOrderLaser iOrderLaser = list.get(0);

            //判断是否全部打印完成
            if(productionIsEndByOrderNo(iOrderLaser.getOrderNo()) == 0){
                String orderNo = iOrderLaser.getOrderNo();
                //添加订单日志，生产完成
                OrderLog orderLog = new OrderLog();
                orderLog.setOrderTime(TimeUtils.getNowTime());
                orderLog.setOrderId(orderNo);
                orderLog.setProduction("生产完成");
                orderLogService.save(orderLog);
                //根据订单号修改待生产订单为已生产
                updateStayByOrderIdToState(orderNo);
                //根据订单号修改订单表的状态为已生产
                updateOrderStatus(orderNo);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }


    /**
     * 手动分配，添加日志
     * @param arr
     * @param orderNo
     * @return
     */
    @Override
    @Transactional
    public boolean distributionPrinterAndLog(String[] arr, String orderNo,String pid) {
        try {
            for(String id : arr){
                updateByIdToLpIdStatus(id,pid);
            }

            //判断是否全部打印完成
            if(productionIsEndByOrderNo(orderNo) == 0){
                //添加订单日志，生产完成
                OrderLog orderLog = new OrderLog();
                orderLog.setOrderTime(TimeUtils.getNowTime());
                orderLog.setOrderId(orderNo);
                orderLog.setProduction("生产完成");
                orderLogService.save(orderLog);
                //根据订单号修改待生产订单为已生产
                updateStayByOrderIdToState(orderNo);
                //根据订单号修改订单表的状态为已生产
                updateOrderStatus(orderNo);
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

        return true;
    }

    /**
     * 根据订单号修改订单表的状态为"以生产"
     * @param orderNo
     * @return
     */
    @Override
    public boolean updateOrderStatus(String orderNo) {
        return orderLaserMapper.updateOrderStatus(orderNo);
    }

}
